package com.example.ui.register;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.R;
import com.example.net.NetRequest;
import com.example.ui.HSCallBack;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Instrumentation.ActivityResult;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("ValidFragment")
public class Register3Fm extends Fragment{
	private String tellphone, password;
	private HSCallBack callback;
	private TextView ra3_title, ra3_tellphone;
	private Button ra3_next, ra3_return;
	private EditText ra3_code, ra3_password, ra3_password2;
	public static boolean canNext=false;
	private String res;
	View v;
	
	
	public Register3Fm(){
		
	}
	public Register3Fm(String tellphone){
		this.tellphone=tellphone;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.a_register3, null);	
		
		//tellphone = Register2Fm.tellphone;
		init(v);
		return v;
	}
	private void init(View v) {		
		ra3_tellphone = (TextView) v.findViewById(R.id.ar3_tellphone);
		ra3_title = (TextView) v.findViewById(R.id.register_title);
		ra3_next = (Button) v.findViewById(R.id.register_next);
		ra3_return = (Button) v.findViewById(R.id.register_return);
		ra3_code = (EditText) v.findViewById(R.id.ar3_code);
		ra3_password = (EditText) v.findViewById(R.id.ar3_password);
		ra3_password2 = (EditText) v.findViewById(R.id.ar3_password2);
		ra3_title.setText("设置密码(3/5)");
		ra3_tellphone.setText("+86" + tellphone);
		ra3_return.setOnClickListener(ra3_ocl);
		ra3_next.setOnClickListener(ra3_ocl);

	}

	OnClickListener ra3_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.register_next:
				if(canNext==true){
				next();
				}else{
					match();
				}
				break;
			case R.id.register_return:
				getActivity().onBackPressed();
				break;

			default:
				break;
			}
		}


	};

	private void next() {
	/*	if(canNext==true){
			RegisterActivity4 register4=new RegisterActivity4();
			FragmentManager fm = getFragmentManager();
			FragmentTransaction ft = fm.beginTransaction();
			ft.replace(R.id.l,register4);
			ft.addToBackStack(null);			
			ft.commit();			
           }
		*/
		
	}
	Runnable r3_run = new Runnable() {

		@Override
		public void run() {
			res = NetRequest.postRequest3(tellphone, ra3_code.getText().toString());
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(res);
				String response = jsonObject.getString("response");

				if (response.equals("true")) {
					canNext=true;
					next();
				} else if (response.equals("code_error")) {
					h.sendEmptyMessage(2);
				}
			} catch (JSONException e) {
				
				e.printStackTrace();
			}
		}
	};
	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {

			case 2:			
				new AlertDialog.Builder(getActivity()).setTitle("提示").setMessage("验证码错误，请重试").show();
				break;

			default:
				break;
			}

		};

	};
  
	private void match() {
		String code = ra3_code.getText().toString();
		String i_password = ra3_password.getText().toString();
		String i_password2 = ra3_password2.getText().toString();
		if (i_password.equals(i_password2)) {
			password = i_password;
			int result = i_password.length();
			int codeLength = code.length();
			if (result > 5) {
				if (codeLength != 6) {
					Toast.makeText(getActivity(), "验证码输入错误", Toast.LENGTH_LONG).show();
				} else {
					code = ra3_code.getText().toString();
					
					new Thread(r3_run).start();
					
				}

			} else {
				Toast.makeText(getActivity(), "密码长度不能少于6位", Toast.LENGTH_LONG).show();
			}

		} else {
			Toast.makeText(getActivity(), "两次输入密码不一致", Toast.LENGTH_LONG).show();
		}

	}

}
